package hong.jun.pet_home.controller.pet;

import com.alibaba.fastjson.JSON;
import hong.jun.basic.util.AjaxResult;
import hong.jun.basic.util.PageList;
import hong.jun.basic.util.RedisUtils;
import hong.jun.pet_home.domain.pet.Adopt;
import hong.jun.pet_home.domain.user.User;
import hong.jun.pet_home.query.AdoptQuery;
import hong.jun.pet_home.service.pet.IAdoptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * restfull风格
 *
 * @Controller
 * @ResponseBody
 */

@RestController
@RequestMapping("/adopt")
public class AdoptController {

    @Autowired
    private IAdoptService adoptService;

    /**
     * 判断有无id决定新增还是修改
     */
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Adopt adopt) {
        try {
            if (adopt.getId() != null) {
                adoptService.update(adopt);
            } else {
                adoptService.save(adopt);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.toString());
        }
    }

    /**
     * 入库
     */
    @PutMapping("/Warehousing")
    public AjaxResult Warehousing(@RequestBody Adopt adopt) {
        try {
            adoptService.Warehousing(adopt);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.toString());
        }
    }

    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody Adopt adopt, HttpServletRequest request) {
        try {
            User user = JSON.parseObject(RedisUtils.INSTANCE.get(request.getHeader("U-TOKEN")), User.class);
            adopt.setUser(user);
            adopt.setState(0);
            adoptService.publish(adopt);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.toString());
        }
    }

    /**
     * 根据id 删除
     */
    @DeleteMapping(value = "/{id}")
    public AjaxResult del(@PathVariable("id") Long id) {
        try {
            //System.out.println(id);
            Adopt adopt = adoptService.getById(id);
            System.out.println("删除" + adopt.toString());//未查到对象为空会报空指针异常
            adoptService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.toString());
        }
    }

    /**
     * 根据id 批量删除
     */
    @PostMapping("/dels")
    public AjaxResult dels(@RequestBody Long[] ids) {
        try {
            System.out.println(ids.toString());
            adoptService.deletes(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(e.toString());
        }
    }

    /**
     * 根据Id查询
     */
    @GetMapping(value = "/{id}")
    public Adopt getById(@PathVariable("id") Long id) {
        return adoptService.getById(id);
    }

    /**
     * 查询上架的全部商品
     */
    @PostMapping("/getAll")
    public List<Adopt> getAll() {
        return adoptService.getAll();
    }

    /**
     * 条件分页查询
     */
    @PostMapping
    public PageList<Adopt> getPage(@RequestBody AdoptQuery query) {
        // System.out.println("条件"+query);
        return adoptService.queryPage(query);
    }
}
